<?php
namespace app\controller\back;

use app\common\controller\_AdminController;
use app\model\Admin;
use app\model\Role;
use think\Validate;

class AdminController extends _AdminController
{	
	protected $rule = [
		'id|编号'			=> 'integer',
		'name|登录账号'		=> 'alphaDash|length:3,50',
		'password|登录密码'	=> 'alphaDash|length:3,64',
		'repassword|密码确认'	=> 'confirm:password',
		'status|状态'      	=> 'in:0,1',
		'role_id|角色编号' 	=> 'integer',
		'page|页码'         	=> 'integer',
        'size|条数'        	=> 'integer',
	];
	
	protected $scene = [
		'create'			=> ['name','password','status','role_id'],
		'update'			=> ['id','password','status','role_id'],
		'setPassword'		=> ['password','repassword'],
        'delete'            => ['id'],
        'list'              => ['id','name','role_id','status','page','size'],
        'detail'            => [],
	];

	protected function initialize()
	{	
		parent::initialize();
		switch ($this->action) {
			case 'create':
				$this->appendRule('name','require|unique:admin');
				$this->appendRule('password','require');
				$this->appendRule('role_id','require|checkRoleById');
				break;
			case 'update':
				$this->appendRule('id','require|checkAdminById');
				$this->appendRule('role_id','checkRoleById');
				break;
			case 'setPassword':
				$this->appendRule('password','require');
				$this->appendRule('repassword','require');
				break;
            case 'delete':
                $this->appendRule('id','require|checkAdminDel');
                break;
            case 'list':
                $this->appendRule('id','checkAdminById');
                $this->removeRule('name','length:3,50');
                $this->appendRule('role_id','checkRoleById');
                $this->replaceRule('status','in:0,1','in:0,1,2,-1');
                break;
			default:
				# code...
				break;
		}
		$this->check();
	}
	/**
	 * @api {POST} /back/admin/create 添加管理员
	 * @apiVersion 0.0.1
	 * @apiGroup Back-admin
	 *
	 * @apiParam {String} name 名称
	 * @apiParam {String} password 密码
     * @apiParam {String} repassword 密码确认
	 * @apiParam {Number} role_id 角色ID
	 * @apiParam {Number=0禁用,1启用} [status=1] 状态
	 *
	 * @apiParamExample {json} Request-Example:
	 * {
	 *     "name": "ad222",
	 *     "password": "1234561",
	 *     "repassword": "123456",
	 *     "status": "1",
     *     "role_id": "1",   
	 * }
	 * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []
	 * }
	 */
	public function createAction()
	{   
		Admin::doSave($this->data);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/admin/set_password 设置自己的密码
	 * @apiVersion 0.0.1
	 * @apiGroup Back-admin
	 *
	 * @apiParam {String} password 密码
     * @apiParam {String} repassword 密码确认
     *
     * @apiParamExample {json} Request-Example:
     * {
     *     "password": "1234561",
     *     "repassword": "123456",
     * }
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function setPasswordAction()
	{   
		Admin::doSave($this->data,$this->user['id']);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/admin/update 修改管理员信息
	 * @apiVersion 0.0.1
	 * @apiGroup Back-admin
	 *
	 * @apiParam {Number} id 编号
	 * @apiParam {String} [name] 名称
	 * @apiParam {String} [password] 密码
	 * @apiParam {Number=0禁用,1启用} [status] 状态
     * @apiParam {Number} [role_id] 角色
     *
     * @apiParamExample {json} Request-Example:
     * {
     *     "id": "1",
     *     "name": "ad222",
     *     "password": "1234561",
     *     "status": "1",
     *     "role_id": "1",   
     * }
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function updateAction()
	{   
		Admin::doSave($this->data,$this->id);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/admin/delete 删除管理员
	 * @apiVersion 0.0.1
	 * @apiGroup Back-admin
	 *
	 * @apiParam {Number} id 编号
     *
     * @apiParamExample {json} Request-Example:
     * {
     *     "id": "1",
     * }
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function deleteAction()
	{   
		$error = Admin::doDelete($this->id);
		if($error){
            parent::resError($error);
        }
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/admin/list 管理员列表
	 * @apiVersion 0.0.1
	 * @apiGroup Back-admin
	 * 
	 * @apiParam {Number} [id] 编号
	 * @apiParam {String} [name] 名称
	 * @apiParam {Number=-1删除,0禁用,1启用,2禁用+启用,-1删除} [status=2] 状态
	 * @apiParam {Number} [page=1] 页码
	 * @apiParam {Number} [size=10] 每页条数
     * 
     * @apiParamExample {json} Request-Example:
     * {
     *     "id": "1",
     *     "name": "ad222",
     *     "status": "2",
     *     "page": "1",
     *     "size": "10",   
     * }
     * 
	 * @apiSuccess {Number} id 编号
	 * @apiSuccess {Number} role_id 角色编号
	 * @apiSuccess {String} role 角色名称
	 * @apiSuccess {String} name 名称
     * @apiSuccess {Number} status 状态
     * @apiSuccess {String} status_text 状态值
	 * @apiSuccess {String} last_login_time 最后登录时间
	 * @apiSuccess {String} create_time 创建时间
	 * @apiSuccess {String} update_time 更新时间
	 * @apiSuccess {String} count 总条数
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": [{
     *         "id": 39,
     *         "role_id": 1,
     *         "role": "管理员",
     *         "name": "ad1111",
     *         "last_login_time": "2019-04-06 10:31:14",
     *         "status": 1,
     *         "create_time": "2019-04-09 17:24:19",
     *         "update_time": "2019-04-09 17:24:19",
     *         "status_text": "正常"
	 *     }],
	 *     "count": 6
	 * }
	 */
	public function listAction()
	{   
		$cond = Admin::getStatusCond($this->data);
        if( !empty($this->id) ){
            $cond[] = ['id','=',$this->id];
        }
        if( !empty($this->data['role_id']) ){
            $cond[] = ['role_id','=',$this->data['role_id']];
        }
        if(isset($this->data['name']) && strlen($this->data['name']) > 0){
            $cond[] = ['','exp',Admin::raw("locate('{$this->data['name']}',name)>0")];
        }
		$list = Admin::doList($cond,false,$this->page,$this->size);
		return parent::resSuccess($list);
	}
	
	/**
	 * @api {POST} /back/admin/detail 管理员自己的详情
	 * @apiVersion 0.0.1
	 * @apiGroup Back-admin
	 *
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": {
	 *         "role_id": 1,
     *         "role": "管理员",
     *         "name": "ad1111",
     *         "last_login_time": "2019-04-06 10:31:14",
     *         "status": 1,
     *         "create_time": "2019-04-09 17:24:19",
     *         "update_time": "2019-04-09 17:24:19",
     *         "status_text": "正常"
	 *     }   
	 * }
	 */
	public function detailAction()
	{   
        return parent::resSuccess($this->user);
	}

	// 自定义规则
	protected function checkAdminById()
	{
		Validate::extend('checkAdminById', function ($value) {
			$user = Admin::get($value);
			if(!$user){
				return '记录不存在';
			}
			return true;
		});
	}

	// 自定义规则
	protected function checkRoleById()
	{
		Validate::extend('checkRoleById', function ($value) {
			$role = Role::get($value);
			if(!$role){
				return '角色不存在';
			}
			return true;
		});
	}

	// 自定义规则
	protected function checkAdminDel()
	{
		Validate::extend('checkAdminDel', function ($value) {
			$user = Admin::get($value);
			if(!$user){
				return '记录不存在';
			}
	        if($user->status == Admin::STATUS_ENABLE){
	            return '删除前请先禁用';
	        }
	        return true;
		});
	}
}
